//
//  MonitorInfoFirstView.m
//  ICrEAM
//
//  Created by kuroro on 16/11/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MonitorInfoFirstView.h"
#import "ICMTitleSubTitleView.h"
#import "MonitorInfoFirstTableViewCell.h"

@interface MonitorInfoFirstView()
@property (nonatomic,strong) NSMutableArray *labourArray;
@property (nonatomic,strong) NSMutableArray *partArray;
@end

@implementation MonitorInfoFirstView

-(NSMutableArray *)labourArray{
    if (!_labourArray) {
        _labourArray = [NSMutableArray array];
    }
    return _labourArray;
}

-(NSMutableArray *)partArray{
    if (!_partArray) {
        _partArray = [NSMutableArray array];
    }
    return _partArray;
}


- (instancetype)initMonitorInfoFirstViewWithDic:(NSDictionary *)dic{
    if (self = [super init]) {
        __weak typeof(self) weakSelf = self;
        if (IsArr(dic[@"TT_SUGGEST_MAINTAIN_LABOUR"])) {
            self.labourArray = dic[@"TT_SUGGEST_MAINTAIN_LABOUR"];
        }
        if (IsArr(dic[@"TT_SUGGEST_MAINTAIN_PART"])) {
            self.partArray = dic[@"TT_SUGGEST_MAINTAIN_PART"];
        }
        self.backgroundColor = [UIColor whiteColor];
        //头部
        UIView *headerView = [[UIView alloc]init];
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = TitleFont;
        titleLabel.textColor = TitleColor;
        titleLabel.text = @"监控信息";
        [headerView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(headerView.mas_centerX);
            make.centerY.equalTo(headerView.mas_centerY);
        }];
        
        UILabel *pageNum = [[UILabel alloc]init];
        pageNum.font = ContentFont;
        pageNum.textColor = ContentTextColor_Gray;
        pageNum.text = @"(1/3)";
        [headerView addSubview:pageNum];
        [pageNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(titleLabel.mas_bottom);
            make.left.equalTo(titleLabel.mas_right);
        }];
        
        UIView *headerLineView = [[UIView alloc]init];
        headerLineView.backgroundColor = SepColor_gray;
        [headerView addSubview:headerLineView];
        [headerLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headerView.mas_left);
            make.right.equalTo(headerView.mas_right);
            make.bottom.equalTo(headerView.mas_bottom);
            make.height.equalTo(@(1));
        }];
        
        [self addSubview:headerView];
        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top);
            make.left.equalTo(weakSelf.mas_left);
            make.right.equalTo(weakSelf.mas_right);
            make.height.equalTo(@(ActualHeight(65)));
        }];
        
        
        //信息
        UIView *infoView = [[UIView alloc]init];
        infoView.backgroundColor = BackgroundColor_White;
        UIView *ID = [ICMTitleSubTitleView createICMTitleSubTitleViewWithTitle:@"品牌会员" subtitle:dic[@"VW_CARD_ID"]?:@""];
        [infoView addSubview:ID];
        [ID mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(infoView.mas_left).offset(ActualWidth(60));
            make.top.equalTo(infoView.mas_top).offset(ActualHeight(65));
            make.height.equalTo(@(ActualHeight(65)));
            make.width.equalTo(@(ActualWidth(145)));
        }];
        
        UIView *qualityView = [ICMTitleSubTitleView createICMTitleSubTitleViewWithTitle:@"质量担保到期" subtitle:dic[@"ZLDB_END_DATE"]?:@""];
        [infoView addSubview:qualityView];
        [qualityView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(ID.mas_left);
            make.top.equalTo(ID.mas_bottom).offset(ActualHeight(20));
            make.height.equalTo(@(ActualHeight(65)));
            make.width.equalTo(@(ActualWidth(145)));
        }];
        
        UIView *threeGuaranteesView = [ICMTitleSubTitleView createICMTitleSubTitleViewWithTitle:@"三包车辆" subtitle:dic[@"IS_SANBAO"]?:@""];
        [infoView addSubview:threeGuaranteesView];
        [threeGuaranteesView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ID.mas_top);
            make.left.equalTo(ID.mas_right).offset(8);
            make.height.equalTo(@(ActualHeight(65)));
            make.width.equalTo(@(ActualWidth(145)));
        }];
        
        UIView *insuranceView = [ICMTitleSubTitleView createICMTitleSubTitleViewWithTitle:@"保险到期" subtitle:dic[@"INSURATION_END_DATE"]?:@""];
        [infoView addSubview:insuranceView];
        [insuranceView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(threeGuaranteesView.mas_left);
            make.top.equalTo(threeGuaranteesView.mas_bottom).offset(ActualHeight(20));
            make.height.equalTo(@(ActualHeight(65)));
            make.width.equalTo(@(ActualWidth(145)));
        }];
        
        UIView *personelView = [ICMTitleSubTitleView createICMTitleSubTitleViewWithTitle:@"员工车辆" subtitle:dic[@"IS_EMPLOYEE_CAR"]?:@""];
        [infoView addSubview:personelView];
        [personelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(threeGuaranteesView.mas_top);
            make.left.equalTo(threeGuaranteesView.mas_right).offset(8);
            make.height.equalTo(@(ActualHeight(65)));
            make.width.equalTo(@(ActualWidth(150)));
        }];
        
        UIView *firstView = [ICMTitleSubTitleView createICMTitleSubTitleViewWithTitle:@"首次进店" subtitle:dic[@"IS_FIRST_COME"]?:@""];
        [infoView addSubview: firstView];
        [firstView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(personelView.mas_top);
            make.left.equalTo(personelView.mas_right).offset(8);
            make.height.equalTo(@(ActualHeight(65)));
            make.width.equalTo(@(ActualWidth(150)));
        }];
        
        UIView *durationView = [ICMTitleSubTitleView createICMTitleSubTitleViewWithTitle:@"未进店时间（天）" subtitle:dic[@"DEPART_DAYS"]?:@""];
        [infoView addSubview:durationView];
        [durationView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(firstView.mas_top);
            make.left.equalTo(firstView.mas_right).offset(8);
            make.height.equalTo(@(ActualHeight(65)));
            make.width.equalTo(@(ActualWidth(150)));
        }];
        
        UIView *IACSView = [ICMTitleSubTitleView createICMTitleSubTitleViewWithTitle:@"IACS提示" subtitle:dic[@"IACS_INFO"]?:@""];
        [infoView addSubview:IACSView];
        [IACSView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(insuranceView.mas_top);
            make.left.equalTo(personelView.mas_left);
            make.right.equalTo(durationView.mas_right);
            make.height.equalTo(@(ActualHeight(65)));
        }];
        
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = SepColor_gray;
        [infoView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(infoView.mas_left).offset(3);
            make.right.equalTo(infoView.mas_right).offset(3);
            make.bottom.equalTo(infoView.mas_bottom);
            make.height.equalTo(@(2));
        }];
        
        [self addSubview:infoView];
        [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView.mas_bottom);
            make.left.equalTo(weakSelf.mas_left);
            make.right.equalTo(weakSelf.mas_right);
            make.height.equalTo(@(ActualHeight(240)));
        }];
        
        
        //上次维修建议
        UIView *advice = [[UIView alloc]init];
        advice.backgroundColor = BackgroundColor_White;
        
        UILabel *adviceLabel = [[UILabel alloc]init];
        adviceLabel.font = NaviFont;
        adviceLabel.textColor = TitleColor_Dark;
        adviceLabel.text = @"上次维修建议";
        [advice addSubview:adviceLabel];
        [adviceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(advice.mas_left).offset(ActualWidth(60));
            make.top.equalTo(advice.mas_top).offset(ActualHeight(55));
        }];
        
        //维修TableView
        UIView *maintainView = [[UIView alloc]init];
        maintainView.layer.borderColor = SepColor_gray.CGColor;
        maintainView.backgroundColor = [UIColor whiteColor];
        maintainView.layer.borderWidth = 1;
        
        UIView *maintainHeaderView = [self SetUpHeaderViewWithID:@"维修项目代码" name:@"维修项目名称" num:@"工时（执行）" cost:@"预计费用"];
        [maintainView addSubview:maintainHeaderView];
        [maintainHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(maintainView.mas_left).offset(1);
            make.right.equalTo(maintainView.mas_right).offset(-1);
            make.top.equalTo(maintainView.mas_top).offset(1);
            make.height.equalTo(@(ActualHeight(30)));
        }];
        
        self.maintainTabelView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.maintainTabelView.delegate = self;
        self.maintainTabelView.dataSource = self;
        self.maintainTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self.maintainTabelView registerClass:[MonitorInfoFirstTableViewCell class] forCellReuseIdentifier:@"MonitorInfoFirstTableViewCell"];
        [maintainView addSubview:self.maintainTabelView];
        [self.maintainTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(maintainHeaderView.mas_bottom).offset(-1);
            make.bottom.equalTo(maintainView.mas_bottom);
            make.left.equalTo(maintainView.mas_left).offset(1);
            make.right.equalTo(maintainView.mas_right).offset(-1);
        }];

        
        
        
        [advice addSubview:maintainView];
        [maintainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(adviceLabel.mas_bottom).offset(15);
            make.centerX.equalTo(advice.mas_centerX);
            make.width.equalTo(@(ActualWidth(760)+2));
            make.height.equalTo(@(ActualHeight(80)+2));
        }];
        
        //零件
        UIView *partView = [[UIView alloc]init];
        partView.layer.borderColor = SepColor_gray.CGColor;
        partView.backgroundColor = [UIColor whiteColor];
        partView.layer.borderWidth = 1;
        
        UIView *partViewHeaderView = [self SetUpHeaderViewWithID:@"零件代码" name:@"零件名称" num:@"数量" cost:@"预计费用"];
        [partView addSubview:partViewHeaderView];
        [partViewHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(partView.mas_left).offset(1);
            make.right.equalTo(partView.mas_right).offset(-1);
            make.top.equalTo(partView.mas_top).offset(1);
            make.height.equalTo(@(ActualHeight(30)));
        }];
        
        self.partTabelView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.partTabelView.delegate = self;
        self.partTabelView.dataSource = self;
        self.partTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self.partTabelView registerClass:[MonitorInfoFirstTableViewCell class] forCellReuseIdentifier:@"MonitorInfoFirstTableViewCell"];
        [partView addSubview:self.partTabelView];
        [self.partTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(partViewHeaderView.mas_bottom).offset(-1);
            make.bottom.equalTo(partView.mas_bottom);
            make.left.equalTo(partView.mas_left).offset(1);
            make.right.equalTo(partView.mas_right).offset(-1);
        }];
        
        
        
        
        [advice addSubview:partView];
        [partView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(maintainView.mas_bottom).offset(30);
            make.centerX.equalTo(advice.mas_centerX);
            make.width.equalTo(@(ActualWidth(760)+2));
            make.height.equalTo(@(ActualHeight(80)+2));
        }];

        
        
        
        [self addSubview:advice];
        [advice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(infoView.mas_bottom);
            make.left.equalTo(weakSelf.mas_left);
            make.right.equalTo(weakSelf.mas_right);
            make.bottom.equalTo(weakSelf.mas_bottom);
        }];
        
        
     

        
        
    }
    return self;
}

+ (instancetype)creatMonitorInfoFirstViewWithDic:(NSDictionary *)dic{
    return [[MonitorInfoFirstView alloc]initMonitorInfoFirstViewWithDic:dic];
}


- (UIView *)SetUpHeaderViewWithID:(NSString *)ID name:(NSString *)name num:(NSString *)num cost:(NSString *)cost {
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = TitleColor;
    UILabel *IDLabel = [self createLabelWithText:ID textAlignment:NSTextAlignmentCenter];
    [headerView addSubview:IDLabel];
    [IDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(headerView.mas_left);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(ActualWidth(280)));
    }];
    UIView *lineView1 = [[UIView alloc]init];
    lineView1.backgroundColor = SepColor_gray;
    [headerView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(IDLabel.mas_right).offset(-1);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(1));
    }];
    
    UILabel *nameLabel = [self createLabelWithText:name textAlignment:NSTextAlignmentCenter];
    [headerView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(IDLabel.mas_right);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(ActualWidth(270)));
    }];
    
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = SepColor_gray;
    [headerView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(nameLabel.mas_right).offset(-1);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(1));
    }];
    
    UILabel *timeLabel= [self createLabelWithText:num textAlignment:NSTextAlignmentCenter];
    [headerView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(nameLabel.mas_right);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(ActualWidth(110)));
    }];
    UIView *lineView3 = [[UIView alloc]init];
    lineView3.backgroundColor = SepColor_gray;
    [headerView addSubview:lineView3];
    [lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(timeLabel.mas_right).offset(-1);
        make.bottom.equalTo(headerView.mas_bottom);
        make.width.equalTo(@(1));
    }];
    
    UILabel *costLabel = [self createLabelWithText:cost textAlignment:NSTextAlignmentCenter];
    [headerView addSubview:costLabel];
    [costLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_top);
        make.left.equalTo(timeLabel.mas_right);
        make.bottom.equalTo(headerView.mas_bottom);
        make.right.equalTo(headerView.mas_right);
    }];
    
    
    
    return headerView;
}

- (UILabel *)createLabelWithText:(NSString *)text  textAlignment:(NSTextAlignment)textAlignment{
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.font = ContentFont;
    label.textColor = ContentTextColor_Black;
    label.textAlignment = textAlignment;
    return label;
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.maintainTabelView) {
        return self.labourArray.count;
    }else{
        return self.partArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MonitorInfoFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MonitorInfoFirstTableViewCell"];
    if (tableView == self.maintainTabelView) {
        NSDictionary *dic = self.labourArray[indexPath.row];
          [cell SetUpHeaderViewWithID:dic[@"LABOUR_CODE"] name:dic[@"LABOUR_NAME"] num:dic[@"STD_LABOUR_HOUR"] cost:dic[@"LABOUR_AMOUNT"]];
    }else{
        NSDictionary *dic = self.partArray[indexPath.row];
          [cell SetUpHeaderViewWithID:dic[@"PART_NO"] name:dic[@"PART_NAME"] num:dic[@"QUANTITY"] cost:dic[@"PART_AMOUNT"]];
    }
  
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ActualHeight(25);
}


@end
